using UnityEngine;

public class EnemyPatrollerBehaviour : MonoBehaviour
{
    private Rigidbody rb;
    private EnemyFlip enemyFlip;
    private AnimationController anim;
    private Transform player;

    [Header("Movement Settings")]
    [SerializeField] private float moveSpeed = 1f;
    [SerializeField] private float animationLength = 2f;
    [SerializeField] private float animationDistance = 1f;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
        enemyFlip = GetComponent<EnemyFlip>();
        anim = GetComponent<AnimationController>();
    }

    private void Start()
    {
        GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
        if (playerObj != null)
        {
            player = playerObj.transform;
        }
    }

    private void FixedUpdate()
    {
        FollowPlayer();
    }

    private void FollowPlayer()
    {
        if (player == null)
            return;

        Vector3 toPlayer = player.position - transform.position;
        toPlayer.y = 0f;

        Vector3 direction = toPlayer.normalized;

        anim.SetAnim("walk", moveSpeed, animationDistance, animationLength);

        enemyFlip.LookPlayer(direction);

        rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
    }
}